﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WinHBM.Core.Net.Http;
using WinHBM.Core.Net.Sockets.Cmd;
using System.Security.Cryptography.X509Certificates;
using WinHBM.Utils;

namespace WinHBM.Core.Task
{
    public class GetTokenTask
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(GetTokenTask));
        private static Thread loginThread = null;

        public static void Start()
        {
            if(TokenHolder.isLogin)
            {
                Logger.Debug("alreadly login; GetTokenTask exit");
                return;
            }

            if (!File.Exists("user.bin"))
            {
                Logger.Debug("user.bin not exist; GetTokenTask exit");
                return;
            }

            loginThread = new Thread(GetTokenTask.DoTask);
            loginThread.IsBackground = true;
            loginThread.Start();
        }

        public static void Stop()
        {
            if(loginThread == null || loginThread.IsAlive == false)
            {
                return;
            }

            loginThread.Abort();
        }



        public static void DoTask()
        {
            FileStream fs = null;
            UserInfo user = null;
            
            try
            {
                //读取文件流对象 
                user = UserInfo.INSTANCE;
                string mobile = user.LoginId;
                string password = user.Password;
                if(mobile == null || mobile == "")
                {
                    Logger.Debug("mobile is empty");
                    return;
                }
                HttpClient client = HttpClient.Instance;
                JObject requestBody = new JObject();
                requestBody["login_id"] = mobile;
                requestBody["password"] = password;
                requestBody["role"] = "teacher";
                JObject result = UtilRequest.DoRequest("/api/auth/login", RequestMethod.POST, requestBody);
                if (result["errcode"].ToString() != "0")
                {
                    Logger.Debug(result["message"]);
                    return;
                }
                //存储Token
                string token = result["contents"]["token"].ToString();
                TokenHolder.Token = token;
                TokenHolder.isLogin = true;
                
                Logger.Debug("login success");

            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Flush();
                    fs.Close();
                }

            }

        }
        
    }
}
